package com.owen.redis.redisson.distributed.lock.service.impl;

import com.owen.redis.redisson.distributed.lock.service.TestService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void lock() {
        RLock test = redissonClient.getLock("test");
        try {
            test.lock(60, TimeUnit.SECONDS);
            System.out.println("=========lock========");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            test.unlock();
        }
    }
}
